﻿using System;
using System.Collections.Specialized;
using CRS.Core.Entities;
using VitCode.Castle.DataAccess;
using System.Data;
using System.Collections.Generic;

namespace CRS.Core.DataAccess
{
    public class CoreDataAccess:BaseDataAccess
    {
        #region webService上需要用到的方法(xzr)             

        /// <summary>
        /// 获取指定用户的该用户的盘点单（盘点单号列表）
        /// </summary>
        /// <param name="userCode">PDA登陆用户</param>
        /// <returns>List</returns>
        public List<string> GetInventoryBillNo(string userCode)
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// 得到指定盘点单的盘点货品列表（用于PDA盘点，此列表可能只需：货品条码、货品名称、库存数量等列信息）
        /// </summary>
        /// <param name="billNo">PDA登陆用户</param>
        /// <returns>DataTable</returns>
        public DataTable GetInventoryGoodList(string billNo)
        {
            var sql = "select g.Name,g.Code,eg.Amount from vitcode_castle_User u " +
                      " inner join  crs_core_exgoodsinfo eg  on u.Code='" + billNo + "' and eg.ContactId=u.ContactId " +
                      " inner join crs_core_goodsinfo g on  eg.GoodsId=g.Id order by Code";
            return GetDataTableBySql(sql);
        }

        /// <summary>
        /// 得到当前登陆用户已开局的房间名称（List<包房名称>）
        /// </summary>
        /// <param name="userCode">PDA登录用户</param>
        /// <returns>List</returns>
        public List<string> GetRoomInfo(string userCode)
        {
            return new List<string>();
        }

        /// <summary>
        /// 将盘点结束时多于库存的货品信息列表更新货品的库存信息
        /// </summary>
        /// <param name="moreStockList">多于库存货品的列表（string[]结构：string[条码，数量]）</param>    
        /// <param name="billNo">盘点单号</param>
        /// <returns>DataTable</returns>
        public void UpdateMoreStockInfo(List<string[]> moreStockList,string billNo)
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// 将盘点结束时少于库存的货品信息列表更新货品的库存信息
        /// </summary>
        /// <param name="lessStockList">少于库存货品的列表（string[]结构：string[条码，数量]）</param>   
        /// <param name="billNo">盘点单号</param>
        /// <returns>DataTable</returns>
        public void UpdateLessStockInfo(List<string[]> lessStockList,string billNo)
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// 将某个房间的消费货品列表更新到相应的包房消费中
        /// </summary>
        /// <param name="consumptionList">消费货品列表（string[]结构：string[条码，数量]）</param>
        /// <param name="roomName">包房号</param>
        /// <param name="userCode">PDA登陆用户</param>
        /// <returns>DataTable</returns>
        public void GetConsumptionList(List<string[]> consumptionList,string roomName,string userCode)
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// 将零售货品列表更新零售货品信息
        /// </summary>
        /// <param name="retailList">零售货品列表（string[]结构：string[条码，数量]）</param>
        /// <param name="userCode">PDA登陆用户</param>
        /// <returns>DataTable</returns>
        public void GetRetailList(List<string[]> retailList,string userCode)
        {
            throw new System.NotImplementedException();
        }
        #endregion


        public string CreateCurrRoomCostData(string goodsCode,Guid contactId,int amount,Guid roundId)
        {
            var errMsg = "";
            var nvc = new NameValueCollection {{"Goods.Code", goodsCode}, {"Contact.Id", contactId.ToString()},{"IsLast","1"}};
            var objExGoods = GetSingleObjectByProVals(typeof (ExGoodsInfo).Name, nvc,true,true);
            if (objExGoods == null)
                errMsg = "找不到条码对应的货品";
            else
            {
                var exGoods = ((ExGoodsInfo) objExGoods);
                if (exGoods.Amount < amount)
                    errMsg = "【"+exGoods.Goods.Name+"】没有足够的库存";
                nvc = new NameValueCollection {{"ExGoodsInfoId", exGoods.Id.ToString()}, {"RoundInfoId", roundId.ToString()}};
                var objCostItem = GetSingleObjectByProVals(typeof (CostItemInfo).Name, nvc, true,true);
                CostItemInfo costItem=null;

                if (objCostItem == null)
                {
                    costItem = new CostItemInfo();
                    costItem.ExGoodsInfoId = exGoods.Id;
                }
                else
                    costItem = ((CostItemInfo) objCostItem);
                costItem.RoundInfoId = roundId;
                costItem.IsConfirm = false;
                costItem.CostType = CostItemInfo.CT_ROOM_COST;
                costItem.CostDate = DateTime.Now;
                costItem.Quality += amount;
                SaveOrUpdate(costItem);
            }
            return errMsg;
        }

        public string CancelCurrRoomCostItemAmount(string costItemId,int amount)
        {
            var errMsg = "";
            var objCostItem = GetSingleObjectById(typeof(CostItemInfo).Name,costItemId);
            if (objCostItem == null)
                errMsg="消费记录不存在";
            else
            {
                var costItem = ((CostItemInfo) objCostItem);
                if (costItem.Quality > amount)
                {
                    costItem.Quality-=amount;
                    SaveOrUpdate(costItem);
                }
                else
                {
                    Delete(costItem);
                }
            }
            return errMsg;
        }

        public int CancelCurrRoomCostRoundItem(string roundId)
        {
            var reVal = 0;
            var hql = "From CostItemInfo o where o.RoundInfoId='"+roundId+"' and IsConfirm=0";
            reVal = Session.Delete(hql);
            return reVal;
        }


        public string ConfirmCurrRoundCostItems(string roundId)
        {
            return "";
        }

        public DataTable GetCostList(Guid roundId,bool isConfirm)
        {
            var sql =
                " select c.id,g.Code 货品条码,g.Name 货品名称, "+
                " gt.Name 类别,c.CostType 消费类型, "+
                " eg.Price 价格, c.Quality 数量, "+
                " eg.Price * c.Quality 金额 from CRS_Core_CostItemInfo c "+
                " inner join Crs_Core_ExGoodsInfo eg on eg.Id=c.ExGoodsInfoId and c.RoundInfoId='"+roundId+"' and IsConfirm="+Convert.ToInt32(isConfirm)+
                " inner join Crs_Core_GoodsInfo g on g.Id=eg.GoodsId "+
                " inner join Crs_Core_GoodsType gt on gt.Id=g.TypeId Order by c.CostDate desc ";
            return GetDataTableBySql(sql);
        }

        public RoundInfo GetRoundInfo(Guid roomId)
        {
            var obj = GetSingleObjectByProVals(typeof (RoundInfo).Name,
                                               new NameValueCollection
                                                   {{"Room.Id", roomId.ToString()}, {"IsCurrent", "1"}},true,true);
            if (obj != null && obj is RoundInfo)
                return ((RoundInfo) obj);
            return null;
        }
    }
}
